package Login;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/login")
public class loginServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        resp.setContentType("text/html;charset=utf8");
        if ("admin".equals(username) && "123456".equals(password)){
            // 1.创建会话
            HttpSession session = req.getSession(true);
            // 2.保存用户信息
            session.setAttribute("username","admin");
            // 3.重定向
            resp.sendRedirect("index");
        }else {
            // 登录失败
            resp.setStatus(403);
            resp.getWriter().write("用户名密码错误!");
        }
    }
}
